package com.up360.rapidcalc.aerolite.servers
{
    import com.up360.rapidcalc.aerolite.AeroliteCrisis;
    import com.up360.rapidcalc.aerolite.consts.MyGlobal;
    import com.up360.rapidcalc.aerolite.model.Aerolite;
    import com.up360.rapidcalc.aerolite.util.Dimension;
    
    import flash.geom.ColorTransform;
    import flash.geom.Point;

    /**
     * @author Tunie
     * @date   May 3, 2013
     */
    public class AeroliteServer
    {
        /**
         * 高度的范围
         */
        public static const YLIMIT:Number=-50;
        
        private var _aerolite:Aerolite;
        private var _nextCanMove:Boolean;
        
        public function AeroliteServer()
        {
            _nextCanMove=true;
        }
        
        /**
         * 随机生成起点
         */
        public function randomOutset():void
        {
            //随机生成也是在一定的范围内随机的,取得此范围
            var b_outSet:Point=new Point();
            b_outSet.x=int(Math.random()*MyGlobal.stage.stageWidth);
            b_outSet.y=int(Math.random()*YLIMIT);
            _aerolite.outSet=b_outSet;
            //判断是否与已有陨石重合
            if(AeroliteCrisis.instance.aerolitesServer.testHit(_aerolite))
            {
                randomOutset();
            }else{
                resetFinish();
                updateSpeed();
            }
        }
        
        /**
         * 重新设置终点
         */
        private function resetFinish():void
        {
            var b_aeroLocal:Point=new Point(_aerolite.x,_aerolite.y);
            //斜边
            var b_Hypotenuse:Number=Point.distance(b_aeroLocal,_aerolite.finish);
            var b_straightEdgeX:Number=b_aeroLocal.x-_aerolite.finish.x;
            var b_straightEdgeY:Number=_aerolite.finish.y-b_aeroLocal.y;
            //
            var b_aeroHypotenuse:Number=0;
            b_aeroHypotenuse=Math.pow(_aerolite.width/3,2);
            b_aeroHypotenuse+=Math.pow(_aerolite.height/3,2);
            b_aeroHypotenuse=Math.sqrt(b_aeroHypotenuse);
            //
            _aerolite.finish.x+=b_straightEdgeX*(b_aeroHypotenuse/b_Hypotenuse);
            _aerolite.finish.y-=b_straightEdgeY*(b_aeroHypotenuse/b_Hypotenuse);
        }
        /**
         * 更新陨石的速度
         * 根据起点、终点及剩余时间计算陨石在x、y轴上的增量
         */
        public function updateSpeed():void
        {
            var b_remainTime:Number=_aerolite.hitTime-_aerolite.costedTime;
            var b_xVal:Number=_aerolite.finish.x-_aerolite.outSet.x;
            var b_yVal:Number=_aerolite.finish.y-_aerolite.outSet.y;
            _aerolite.speed.width=b_xVal/b_remainTime*AerolitesServer.DELAY;
            _aerolite.speed.height=b_yVal/b_remainTime*AerolitesServer.DELAY;
        }
        
        public function randomColor():void
        {
            var b_tint:Number=getRandomNum(0.2,0.5);
            var b_red:int=getRandomNum(100,250)*b_tint;
            var b_green:int=getRandomNum(120,180)*b_tint;
            var b_blue:int=getRandomNum(2,25)*b_tint;
            _aerolite.ui.transform.colorTransform=new ColorTransform(1,1,1,1,b_red,b_green,b_blue);
        }
        
        /**
         *取得在指定范围内的数值 
         * @return 
         * 
         */
        private function getRandomNum(small:Number,big:Number):Number
        {
            var b_result:Number=Math.random()*big;
            if(b_result<small) b_result+=small;
            if(b_result>big) b_result=big;
            return b_result;
        }
        
        public function move():void
        {
            if(!nextCanMove)
            {
                //表示陨石已经到达炮台了，提示玩家失败信息
//                trace("玩家失败！");
                AeroliteCrisis.instance.fortBeKill();
                _aerolite.state=Aerolite.BLOWUP;
            }
            else
            {
                var b_nextLocal:Point=new Point();
                b_nextLocal.x=_aerolite.outSet.x+_aerolite.speed.width;
                b_nextLocal.y=_aerolite.outSet.y+_aerolite.speed.height;
                _aerolite.outSet=b_nextLocal;
                _aerolite.costedTime+=AerolitesServer.DELAY;
            }
        }

        private function get nextCanMove():Boolean
        {
            _nextCanMove=true;
            var b_nx:Number=_aerolite.outSet.x+_aerolite.speed.width;
            var b_ny:Number=_aerolite.outSet.y+_aerolite.speed.height;
            if(b_ny>=_aerolite.finish.y)
            {
                _nextCanMove=false;
            }
            return _nextCanMove;
        }

        public function get aerolite():Aerolite
        {
            return _aerolite;
        }

        public function set aerolite(value:Aerolite):void
        {
            _aerolite = value;
        }

        public function clear():void
        {
            aerolite=null;
        }
    }
}